package org.vaadin.golfscoretracker.ui.components;

import java.util.List;

import org.vaadin.appfoundation.i18n.Lang;
import org.vaadin.golfscoretracker.data.Course;
import org.vaadin.golfscoretracker.data.Hole;
import org.vaadin.golfscoretracker.data.Player;
import org.vaadin.golfscoretracker.ui.controller.PlayerController;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Select;
import com.vaadin.ui.TextField;

public class CourseEditor extends CustomField implements ValueChangeListener {

    private static final long serialVersionUID = 3431841291982267768L;

    private final GridLayout mainLayout;

    private Course course;

    public CourseEditor() {
        mainLayout = new GridLayout();
        mainLayout.setSpacing(true);
        setCompositionRoot(mainLayout);

    }

    public void setCourse(Course course) {
        this.course = course;
        mainLayout.removeAllComponents();
        if (course != null) {
            int holes = course.getHoles().size();
            mainLayout.setRows(2);
            mainLayout.setColumns(holes + 1);

            for (int i = 1; i <= holes; i++) {
                Hole hole = course.getHoles().get(i - 1);
                Label holeHeader = new Label();
                holeHeader.setContentMode(Label.CONTENT_XHTML);
                holeHeader.setValue(Lang.getMessage("hole") + " " + i
                        + "<br />" + Lang.getMessage("hole par") + ": "
                        + hole.getPar() + "<br />"
                        + Lang.getMessage("hole length") + ": "
                        + hole.getLength());
                mainLayout.addComponent(holeHeader, i, 0);
            }

            Select select = new Select();
            select
                    .setContainerDataSource(new PlayerController()
                            .getContainer());
            select.setImmediate(true);
            select.addListener(this);
            select.setItemCaptionPropertyId("name");
            mainLayout.addComponent(select, 0, 1);
        }
    }

    @Override
    public Class<?> getType() {
        return List.class;
    }

    @Override
    public void valueChange(Property.ValueChangeEvent event) {
        Player player = (Player) event.getProperty().getValue();
        if (player != null) {
            event.getProperty().setValue(null);
            int rows = mainLayout.getRows();

            Label name = new Label(player.getName());
            mainLayout.replaceComponent((Select) event.getProperty(), name);
            mainLayout.setRows(rows + 1);
            mainLayout.addComponent((Select) event.getProperty(), 0, rows);

            for (int i = 1; i <= course.getHoles().size(); i++) {
                TextField score = new TextField();
                score.setMaxLength(2);
                score.setWidth("40px");
                mainLayout.addComponent(score, i, rows - 1);
            }
        }
    }

}
